﻿Imports System.Data.SqlClient
Imports DTO

Public Class NhanVienDAO

    Public Function LayTheoID(ByVal id As Integer) As NhanVienDTO
        Dim cn As New DataProvider()
        Dim para = New SqlParameter("@MaNV", id)
        Dim dt = cn.ExecuteQuery("LayNhanVienTheoID", para)
        Dim NhanVien As New NhanVienDTO()
        If dt.Rows.Count <> 0 Then
            NhanVien.MaNV = Integer.Parse(dt.Rows(0).Item("MaNV").ToString())
            NhanVien.TenNV = dt.Rows(0).Item("TenNV").ToString()
            NhanVien.SDT = dt.Rows(0).Item("SDT").ToString()
            NhanVien.DiaChi = dt.Rows(0).Item("DiaChi").ToString()
            NhanVien.NgayVaoLam = DateTime.Parse(dt.Rows(0).Item("NgayVaoLam").ToString())
        End If
        Return NhanVien
    End Function


    Public Function LayTatCa() As IList
        Dim cn As New DataProvider()
        Dim array As New ArrayList
        Dim dt = cn.ExecuteQuery("LayAllNhanVien")
        If dt.Rows.Count <> 0 Then
            For Each dr As DataRow In dt.Rows
                Dim NhanVien As New NhanVienDTO()
                NhanVien.MaNV = Integer.Parse(dr.Item("MaNV").ToString())
                NhanVien.TenNV = dr.Item("TenNV").ToString()
                NhanVien.SDT = dr.Item("SDT").ToString()
                NhanVien.NgayVaoLam = DateTime.Parse(dr.Item("NgayVaoLam").ToString())
                NhanVien.DiaChi = dr.Item("DiaChi").ToString()
                array.Add(NhanVien)
            Next
        End If
        Return array
    End Function

    Public Function LayTatCaTT() As DataTable
        Dim cn As New DataProvider()
        Return cn.ExecuteQuery("LayAllNhanVien")
    End Function




    Public Sub Them(ByVal NhanVien As NhanVienDTO)
        Dim cn As New DataProvider()
        Dim para As SqlParameter() = New SqlParameter(3) {}
        para(0) = New SqlParameter("@TenNV", NhanVien.TenNV)
        para(1) = New SqlParameter("@SDT", NhanVien.SDT)
        para(2) = New SqlParameter("@DiaChi", NhanVien.DiaChi)
        para(3) = New SqlParameter("@NgayVaoLam", NhanVien.NgayVaoLam)
        cn.ExecuteNonQuery("InsertNV", para)
    End Sub

    Public Sub CapNhap(ByVal NhanVien As NhanVienDTO)
        Dim cn As New DataProvider()
        Dim para As SqlParameter() = New SqlParameter(4) {}
        para(0) = New SqlParameter("@MaNV", NhanVien.MaNV)
        para(1) = New SqlParameter("@TenNV", NhanVien.TenNV)
        para(2) = New SqlParameter("@SDT", NhanVien.SDT)
        para(3) = New SqlParameter("@DiaChi", NhanVien.DiaChi)
        para(4) = New SqlParameter("@NgayVaoLam", NhanVien.NgayVaoLam)
        cn.ExecuteNonQuery("CapNhapNV", para)
    End Sub


    Public Sub Xoa(ByVal id As Integer)
        Dim cn As New DataProvider()
        Dim para = New SqlParameter("@MaNV", id)
        cn.ExecuteNonQuery("DeleteNV", para)
    End Sub

    Function loadnv() As DataTable
        Dim dt As New DataTable()
        Dim com As New DataProvider()
        dt = com.ExecuteQuery("Loadnv", Nothing)
        Return dt
    End Function

End Class
